Skip to content

fix: remove TxHash caching to prevent stale hash after mutation#107

Merged
icellan merged 1 commit intomasterfrom
fix/remove-txhash-caching
Feb 27, 2026
Merged

fix: remove TxHash caching to prevent stale hash after mutation#107
icellan merged 1 commit intomasterfrom
fix/remove-txhash-caching

Conversation

@icellan
Copy link
Copy Markdown
Contributor

@icellan icellan commented Feb 27, 2026

Summary

  • Remove the cachedHash field from MsgTx that was introduced in v1.2.0
  • Keep the streaming TxHash optimization (writing directly to sha256.New() instead of intermediate buffer)

Problem

The cachedHash field caused two issues:

  1. Stale hash after in-place mutationtxsort.InPlaceSort (BIP 69) reorders inputs/outputs on an existing MsgTx, but the cached hash was not invalidated since cachedHash is unexported. Subsequent TxHash() calls returned the pre-sort hash.

  2. reflect.DeepEqual failures — Any code comparing MsgTx structs via reflect.DeepEqual would fail if one had been hashed (populating the cache) and the other had not.

Why removal is safe

Profiling on a 3.64 GB testnet block (28,672 transactions) confirmed caching provided zero measurable benefit — each transaction hash is typically computed only once during block processing. Results with and without caching are identical:

Metric With caching Without caching
Total Allocated 18,522 MB 18,521 MB
Elapsed Time ~59s ~59s

The real optimization was the streaming hash (eliminating the intermediate bytes.Buffer), which is preserved.

Test plan

  • All go-wire tests pass
  • teranode TestHandleBlockDirect_TestnetLargeBlock passes with no regression

@icellan icellan requested a review from mrz1836 as a code owner February 27, 2026 14:12
@github-actions github-actions bot added the size/S Small change (11–50 lines) label Feb 27, 2026
@github-actions github-actions bot added the bug-P3 Lowest rated bug, affects nearly none or low-impact label Feb 27, 2026
The cachedHash field on MsgTx caused two issues:
- reflect.DeepEqual comparisons failed when one MsgTx had been hashed
  and the other had not
- In-place transaction sorting (e.g. BIP 69) returned stale cached
  hashes since the cache was not invalidated on slice reordering

Profiling showed caching provided no measurable benefit since each
transaction hash is typically computed only once. The streaming hash
optimization (writing directly to sha256.New()) is preserved.
@icellan icellan force-pushed the fix/remove-txhash-caching branch from 5475e8d to c34b9fe Compare February 27, 2026 14:19
@sonarqubecloud
Copy link
Copy Markdown

@icellan icellan merged commit c322413 into master Feb 27, 2026
44 checks passed
@github-actions github-actions bot deleted the fix/remove-txhash-caching branch February 27, 2026 14:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug-P3 Lowest rated bug, affects nearly none or low-impact size/S Small change (11–50 lines)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants